package com.valkyries.jhhj.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.valkyries.jhhj.common.Result;
import com.valkyries.jhhj.domain.model.LoginBody;
import com.valkyries.jhhj.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 登录操作处理
 */
@RestController
@RequestMapping("/sys/login/")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * post 登录请求
     */
    @PostMapping("/doLogin")
    public Result doLogin(@RequestBody LoginBody loginBody) {
        return loginService.login(loginBody);
    }

    /**
     * 验证登录状态
     */
    @RequestMapping("/isLogin")
    public Result isLogin() {
        return Result.ok("当前会话是否登录：" + StpUtil.isLogin());
    }


    /**
     * 获取token信息
     */
    @GetMapping("/tokenInfo")
    public Result tokenInfo() {
        return Result.data(StpUtil.getTokenInfo());
    }

    /**
     * 注销登录
     */
    @PostMapping("/logout/{userId}")
    public Result logout(@PathVariable("userId") Integer userId){
        StpUtil.logout(userId);
        return Result.ok("退出登录成功");
    }

}
